using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using DataLoad.iContact.WinClient.Properties;

namespace DataLoad.iContact.WinClient
{
    public partial class frmLogin : Form
    {
        private frmLogin()
        {
            InitializeComponent();
        }

        public string Username
        {
            get
            {
                return edtUsername.Text;
            }
        }

        public string Password
        {
            get
            {
                return edtPassowrd.Text;
            }
        }

        public string Token
        {
            get
            {
                return edtCaptcha.Text;
            }
        }

        private string _captchaToken;

        public string CaptchaToken
        {
            get
            {
                return _captchaToken;
            }
            set
            {
                _captchaToken = value;
            }
        }

        public bool CaptchaRequired
        {
            get
            {
                return (!string.IsNullOrEmpty(_captchaToken));
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            LoginManager.Username = edtUsername.Text;
            LoginManager.Password = edtPassowrd.Text;
            LoginManager.RememberLogin = chkRemember.Checked;

            if (CaptchaRequired)
            {
                LoginManager.CaptchaAnswer = edtCaptcha.Text;
                LoginManager.CaptchaToken = _captchaToken;
            }
            else
            {
                LoginManager.CaptchaAnswer = string.Empty;
                LoginManager.CaptchaToken = string.Empty;
            }

            LoginManager._captchaRequired = CaptchaRequired;

            LoginManager.Save();
        }



        private void frmLogin_Load(object sender, EventArgs e)
        {
            edtUsername.Text = LoginManager.Username;
            if (LoginManager.RememberLogin)
            {
                edtPassowrd.Text = LoginManager.Password;
            }
            else
            {
                edtPassowrd.Text = string.Empty;
            }
            
            chkRemember.Checked = LoginManager.RememberLogin;
        }

        public static DialogResult Show(string captchaToken)
        {
            using (frmLogin frm = new frmLogin())
            {
                frm.CaptchaToken = captchaToken;
                if (frm.CaptchaRequired)
                {
                    frm.grpCaptcha.Show();
                    frm.picCaptcha.Load(string.Format("http://www.google.com/accounts/Captcha?ctoken={0}", captchaToken));
                }
                else
                {
                    frm.Height = frm.Height - frm.grpCaptcha.Height;
                    frm.grpCaptcha.Hide();
                }

                return frm.ShowDialog();
            }
        }

        public new static DialogResult Show()
        {
            return Show(string.Empty);
        }
    }
}